/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdl_newContract.java
 *
 * Created on Aug 23, 2010, 9:10:16 AM
 */

package GUI;

import config.Database_function;
import config.Function;
import config.User_function;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author RYUDO
 */
public class jdl_newContract extends javax.swing.JDialog {

    /** Creates new form jdl_newContract */
    public jdl_newContract(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Function.Windows_centered(this.getWidth(), this.getHeight(), this);
    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pnBasic = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtChild = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTeacher = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtStartDate = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        txtEndDate = new com.toedter.calendar.JDateChooser();
        lbl_infoChild = new javax.swing.JLabel();
        lbl_Info_teacher = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add new contract");

        jPanel1.setBackground(new java.awt.Color(213, 228, 242));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setForeground(new java.awt.Color(9, 32, 90));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/new-contract.png"))); // NOI18N
        jLabel1.setText("Add New Contract");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        pnBasic.setBackground(new java.awt.Color(213, 228, 242));
        pnBasic.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Child ID:");

        txtChild.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtChildFocusLost(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Nanny ID:");

        txtTeacher.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTeacherActionPerformed(evt);
            }
        });
        txtTeacher.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTeacherFocusLost(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Start Date:");

        txtStartDate.setDateFormatString("MMMM/dd/yyyy");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("End Date:");

        txtEndDate.setDateFormatString("MMMM/dd/yyyy");

        lbl_infoChild.setFont(new java.awt.Font("Tahoma", 1, 11));

        lbl_Info_teacher.setFont(new java.awt.Font("Tahoma", 1, 11));

        javax.swing.GroupLayout pnBasicLayout = new javax.swing.GroupLayout(pnBasic);
        pnBasic.setLayout(pnBasicLayout);
        pnBasicLayout.setHorizontalGroup(
            pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBasicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnBasicLayout.createSequentialGroup()
                        .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTeacher)
                            .addComponent(txtChild, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_Info_teacher)
                            .addComponent(lbl_infoChild)))
                    .addGroup(pnBasicLayout.createSequentialGroup()
                        .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        pnBasicLayout.setVerticalGroup(
            pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnBasicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addGroup(pnBasicLayout.createSequentialGroup()
                        .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtChild, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbl_infoChild))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtTeacher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbl_Info_teacher))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addGroup(pnBasicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pnBasic, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(223, 223, 223)
                        .addComponent(btnSave)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnBasic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSave)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean check() {
        boolean check = true;
        if(this.txtStartDate.getDate() == null) {
            check = false;
            JOptionPane.showMessageDialog(null, "Start Date is not empty");
        } else if (this.txtEndDate.getDate() == null) {
             check = false;
            JOptionPane.showMessageDialog(null, "End Date is not empty");
        }
        else if(this.txtStartDate.getDate().equals(this.txtEndDate.getDate()) || this.txtStartDate.getDate().after(this.txtEndDate.getDate())) {
            check = false;
            JOptionPane.showMessageDialog(null, "Start Date is not equals or after than the End Date");
        } else  if(this.txtEndDate.getDate().before(this.txtStartDate.getDate())) {
            check = false;
            JOptionPane.showMessageDialog(null, "End Date is not before the End Date");
        }
        return check;
    }

    private void txtChildFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtChildFocusLost
        try {
            if(!this.txtChild.getText().equals("")) {
                    ResultSet rs = User_function.get_child_info_by_id(this.txtChild.getText());
                if (rs.first()) {
                    this.lbl_infoChild.setText("Name: "+rs.getString("LastName")+" "+rs.getString("FirstName")+" | Age Group: "+User_function.get_age_group_name_by_id(rs.getString("AgeGroupID")));
                    
                   
                } else {
                    this.lbl_infoChild.setText("Not found or not available");
               
                }
            } else {
                 this.lbl_infoChild.setText("Not found or not available");
                 
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(jdl_newContract.class.getName()).log(Level.SEVERE, null, ex);
            this.lbl_infoChild.setText("Error Connecting to Database");
        }
        
    }//GEN-LAST:event_txtChildFocusLost

   

    private void txtTeacherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTeacherActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTeacherActionPerformed

    private void txtTeacherFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTeacherFocusLost
        try {
            if(!this.txtTeacher.getText().equals("")) {
                    ResultSet rs = User_function.get_nanny_info_by_id(this.txtTeacher.getText());
                if (rs.first()) {
                    this.lbl_Info_teacher.setText("Name: "+rs.getString("LastName")+" "+rs.getString("FirstName"));
                   
                } else {
                    this.lbl_Info_teacher.setText("Not found or not available");
                  
                }
            } else {
                 this.lbl_Info_teacher.setText("Not found or not available");
                   
            }

        } catch (SQLException ex) {
            Logger.getLogger(jdl_newContract.class.getName()).log(Level.SEVERE, null, ex);
            this.lbl_infoChild.setText("Error Connecting to Database");
        }
    }//GEN-LAST:event_txtTeacherFocusLost

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if(check()) {
            try {
                Connection cn = Database_function.setConnection();
                PreparedStatement pst = cn.prepareStatement("INSERT INTO tbl_Child_Nanny ([ChildID],[NannyID],[Startdate],[Enddate]) VALUES (?,?,?,?)");
                pst.setString(1, this.txtChild.getText());
                pst.setString(2, this.txtTeacher.getText());
                pst.setString(3, this.txtStartDate.getCalendar().getTime().toLocaleString());
                pst.setString(4, this.txtEndDate.getCalendar().getTime().toLocaleString());

                Statement st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                int child_assigned = st.executeUpdate("UPDATE tbl_Nanny SET ChildAssigned = ChildAssigned+1 WHERE NannyID = "+this.txtTeacher.getText());


                 if(pst.executeUpdate() != 0 && child_assigned != 0) {
                       JOptionPane.showMessageDialog(this, "Insert successfully");
                       this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "There's some problem with the database");
                 }

            } catch (SQLException ex) {
               JOptionPane.showMessageDialog(this, "Error ! Make sure that you are not enter the duplicate contract");
               ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                jdl_newContract dialog = new jdl_newContract(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbl_Info_teacher;
    private javax.swing.JLabel lbl_infoChild;
    private javax.swing.JPanel pnBasic;
    private javax.swing.JTextField txtChild;
    private com.toedter.calendar.JDateChooser txtEndDate;
    private com.toedter.calendar.JDateChooser txtStartDate;
    private javax.swing.JTextField txtTeacher;
    // End of variables declaration//GEN-END:variables

}
